﻿using System;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using HandyControl.Controls;
using Microsoft.Win32;
using WWLauncher.Constant;
using WWLauncher.Interface;
using WWLauncher.Util;
using WWLauncher.View.Other;
using WWLauncher.ViewModel;

namespace WWLauncher.View.UserControls.Config
{

    /// <summary>
    /// MotionControl.xaml 的交互逻辑
    /// </summary>
    public partial class ThemeControl : UserControl, IChildrenWindow
    {
        private readonly AppConfig appConfig = MainViewModel.Instance().AppConfig;
        private ConfigWindow _parent;
        public ThemeControl(ConfigWindow parent)
        {
            InitializeComponent();
            _parent = parent;
            if (appConfig.ThemeConfig.BgStyle != BGStyle.GradientBac)
            {
                GradientBGConf.Visibility = Visibility.Collapsed;
                ImgBGConf.Visibility = Visibility.Visible;
            }
            else
            {
                ImgBGConf.Visibility = Visibility.Collapsed;
                GradientBGConf.Visibility = Visibility.Visible;
            }
        }

        /// <summary>
        /// 修改背景图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BGButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                OpenFileDialog ofd = new OpenFileDialog
                {
                    Multiselect = false, //只允许选中单个文件
                    Filter = "图像文件(*.png, *.jpg, *.jpge, *.gif)|*.png;*.jpg;*.jpge;*.gif"
                };
                ofd.DefaultDirectory = Constants.AppDir + @"Resource\Wallpaper";

                if (ofd.ShowDialog() == true)
                {
                    appConfig.BitmapImage = ImageUtil.GetBitmapImageByFile(ofd.FileName);
                    appConfig.ImagePath = ofd.FileName;
                    appConfig.BacImgName = ofd.FileName;
                }
            }
            catch (Exception ex)
            {
                LogUtil.WriteErrorLog(ex, "修改背景失败,已重置为默认背景!");
                HandyControl.Controls.Growl.Warning("修改背景失败,已重置为默认背景!", "ThemeGrowl");
            }
            Setting();
        }


        private void DefaultButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                appConfig.BitmapImage = ImageUtil.GetBitmapIconByUri(Constants.DefaultBacImage);
                appConfig.ImagePath = Constants.DefaultBacImage;
                appConfig.BacImgName = "系统默认";
            }
            catch (Exception ex)
            {
                LogUtil.WriteErrorLog(ex, "修改背景失败2,已重置为默认背景!");
                Growl.Warning("修改背景失败,已重置为默认背景!", "ThemeGrowl");
            }
            Setting();
        }


        private void ColorButton_Click(object sender, RoutedEventArgs e)
        {
            string tag = (sender as Button)?.Tag.ToString();
            string strTextColor;
            switch (tag)
            {
                case "Color1":
                    strTextColor = appConfig.ThemeConfig.GradientBgParam.Color1;
                    break;
                case "Color2":
                    strTextColor = appConfig.ThemeConfig.GradientBgParam.Color2;
                    break;
                default:
                    strTextColor = appConfig.ThemeConfig.TextColor;
                    break;
            }
            using (var dialog = new MyColorPickerDialog(tag, "ConfigWindowDialog"))
            {
                dialog.TextColor = strTextColor;
            }
        }


        private void PreviewSlider_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            CheckButtonUpClass cbu = new CheckButtonUpClass
            {
                e = e
            };
            ThreadStart ts = cbu.CheckButtonUp;
            Thread t = new Thread(ts)
            {
                IsBackground = true
            };
            t.Start();
        }

        private class CheckButtonUpClass
        {
            public MouseButtonEventArgs e;

            public void CheckButtonUp()
            {
                while (true)
                {
                    if (e.LeftButton == MouseButtonState.Released)
                    {
                        Application.Current.Dispatcher.Invoke(() =>
                        {
                            var mainViewModel = MainViewModel.Instance();
                            var selectIcons = mainViewModel.SelectedMenuIcons;
                            mainViewModel.SelectedMenuIcons = null;
                            mainViewModel.SelectedMenuIcons = selectIcons;
                        });
                        return;
                    }
                    Thread.Sleep(50);
                }
            }
        }


        public void BGStyle_Changed(object sender, RoutedEventArgs e)
        {
            Setting();
            if (appConfig.ThemeConfig.BgStyle != BGStyle.GradientBac)
            {
                GradientBGConf.Visibility = Visibility.Collapsed;
                ImgBGConf.Visibility = Visibility.Visible;
            }
            else
            {
                ImgBGConf.Visibility = Visibility.Collapsed;
                GradientBGConf.Visibility = Visibility.Visible;
            }
        }

        private void BGOpacity_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            Setting();
        }

        private void Color_TargetUpdated(object sender, DataTransferEventArgs e)
        {
            Setting();
        }
        private void Setting()
        {
            BGSettingUtil.BGSetting(ParentWindow.BGBorder);
            BGSettingUtil.BGSetting(_parent.BGBorder);
        }
        private void SysBG_Click(object sender, RoutedEventArgs e)
        {
            var gbg = new GradientBGDialog
            {
                ConfigWindow = _parent,
                ParentWindow = this.ParentWindow
            };
            gbg.dialog = Dialog.Show(gbg, "ConfigWindowDialog");
        }


        private void Animation_Checked(object sender, RoutedEventArgs e)
        {
            if (ParentWindow.Visibility == Visibility.Collapsed)
            {
                appConfig.IsShow = true;
            }
            appConfig.IsShow = null;
        }

        public MainWindow ParentWindow { get; set; }

        private void ImageWidth_Changed(object sender, RoutedEventArgs e)
        {

        }
    }
}
